package com.yunmang.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.yunmang.R;
import com.yunmang.activity.ScanActivity;
import com.yunmang.app.AppConstant;
import com.yunmang.entity.PackageBean;

import java.util.List;

/**
 * Created by Administrator on 2017/5/3.
 */
public class CommonUtils {

    public static void onScanBarcode(Activity act){
        IntentIntegrator integrator = new IntentIntegrator(act);
        integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
        integrator.setPrompt("扫描条形码");
        integrator.setCameraId(0);
        integrator.setBeepEnabled(true);
        integrator.setCaptureActivity(ScanActivity.class);
        integrator.initiateScan();
    }
    //部门类型 总公司 = 1,分拨中心 = 2,分公司 = 3,分理处 = 4,普通部门 = 5
    public static String getTypeName(int type){
        switch (type){
            case 1:
                return "总公司";
            case 2:
                return "分拨中心";
            case 3:
                return "分公司";
            case 4:
                return "分理处";
            case 5:
                return "普通部门";
        }
        return "";
    }

    public static int getBalanceMethod(String method){
        String balance="0";
        List<PackageBean.ModelBean.BalanceMethodBean> balanceMethodList = AppConstant.balanceMethodList;
        for (PackageBean.ModelBean.BalanceMethodBean bean:balanceMethodList) {
            if(bean.getValue().equals(method)){
                balance=bean.getKey();
                break;
            }
        }
        return Integer.parseInt(balance);
    }

    public static String getNamebyBalance(int type){
        String method="";
        List<PackageBean.ModelBean.BalanceMethodBean> balanceMethodList = AppConstant.balanceMethodList;
        for (PackageBean.ModelBean.BalanceMethodBean bean:balanceMethodList) {
            if(bean.getKey().equals(type+"")){
                method=bean.getValue();
                break;
            }
        }
        return  method;
    }

    public static void onScanQrcode(Activity act){
        IntentIntegrator integrator = new IntentIntegrator(act);
        integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
        integrator.setPrompt("扫描二维码");
        integrator.setCameraId(0);
        integrator.setBeepEnabled(true);
        integrator.initiateScan();

    }
    /**
     * 隐藏键盘
     */
    public static void hideSoftInput(Activity context) {
        InputMethodManager manager;
        manager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (context.getCurrentFocus() != null &&context.getCurrentFocus().getWindowToken() != null) {
            manager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
    /**
     * 校验String是否为空
     *
     * @param etText
     * @return
     */
    public static String getTextViewValue(TextView etText) {
        String etString = etText.getText().toString().trim();
       return etString;
    }

    /**
     * 校验String是否为空
     *
     * @param etText
     * @return
     */
    public static boolean checkTextIsEmpty(TextView etText) {
        String etString = etText.getText().toString().trim();
        if (!TextUtils.isEmpty(etString)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 转换String为Int
     *
     * @param etDeliveryStation
     * @return
     */
    public static int ChangeString2Int(TextView etDeliveryStation) {
        String stringText = etDeliveryStation.getText().toString().trim();
        if (!TextUtils.isEmpty(stringText)) {
            int intText = Integer.parseInt(stringText);
            return intText;
        } else {
            return 0;
        }

    }
    public static int dp2Px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    public static int px2Dp(Context context, float px) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }
    public static TextView message;
    private static Toast t;

    public static void showToast(final Context context, final String text) {
        if (Looper.myLooper() != context.getMainLooper()) {
            //说明不在主线程中
            new Handler(context.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    if (t == null) {
                        View toastRoot = LayoutInflater.from(context).inflate(R.layout.toast, null);
                        message = (TextView) toastRoot.findViewById(R.id.message);
                        message.setText(text);
                        t = new Toast(context);
                        t.setDuration(Toast.LENGTH_SHORT);
                        t.setView(toastRoot);
                    } else {
                        message.setText(text);
                    }
                    t.show();
                }
            });
            return;
        }

        if (t == null) {
            View toastRoot = LayoutInflater.from(context).inflate(R.layout.toast, null);
            message = (TextView) toastRoot.findViewById(R.id.message);
            message.setText(text);
            t = new Toast(context);
            t.setDuration(Toast.LENGTH_SHORT);
            t.setView(toastRoot);
        } else {
            message.setText(text);
        }
//        t.setGravity(Gravity.CENTER, 0, 0);
        t.show();
    }
}
